import { useEffect, useState } from 'react'
import { getUserListApi } from '@/services/userApi'

// 用户列表
export const useUserList = (
  pageNum = 1,
  query: Record<string, any> = {}
): [
  number,
  IUserType.IUserItem[],
  (page: number, query?: Record<string, any>) => void
] => {
  const [data, setData] = useState<IUserType.IUserItem[]>([])
  const [total, setTotal] = useState(0)

  // 加载数据
  const loadData = async (page: number, query: Record<string, any> = {}) => {
    let ret = await getUserListApi(page, query)
    setData(ret.data.users)
    setTotal(ret.data.total)
  }

  useEffect(() => {
    loadData(pageNum, query)
  }, [])

  return [total, data, loadData]
}
